home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Dots & Pixels / headers / screenarea.h < prev    next >
C/C++ Source or Header  |  1995-09-29  |  6KB  |  162 lines

  1. #pragma once
  2. //
  3. // screenarea is a prototypal class for a screen area on the mac.
  4. // It represents a square area of size x size pixels. After creating a
  5. // screenarea one can set pixels on the screen via operator[]. An example:
  6. //
  7. //                screenarea screen( 8);
  8. //                screen.fill();
  9. //                screen[ 3][ 3] = 34;
  10. //
  11. // The screenarea is always allocated on the main screen, and it is assumed
  12. // that that screen is currently in 256-color mode. Furthermore element [ 0][ 0]
  13. // is not located in a corner of the square, but in the center (well, almost in
  14. // the center. One can index the rows of the array by going from -(size / 2)
  15. // up to, but not including size - (size / 2)).
  16. // For reasons of speed no bounds checking is implemented. It is possible
  17. // to create a screenarea which is higher than the main screen without any
  18. // problems, though (but not to create one which is wider than the main screen)
  19. //
  20. // As long as the image bounds on screen are OK it is safe to access the array
  21. // with indices which are reasonably close to the really intended ones. The
  22. // maximum outward offset allowed is screenarea::max_error. This constant also
  23. // determines the maximum half_disparity allowed in 'screendots' (v.i)
  24. //
  25. // 941027: discovered a serious flaw in this hack while implementing 'FormExp'
  26. // The remark regarding 'reasonably close to' is only valid regarding the array's
  27. // x coordinates. BTW: this also means that 'add_big_dot' should not be called
  28. // for dot positions outside the main screen.
  29. //
  30. // 941028: There may be some problems writing outside the 'visible screen area'
  31. // with screenareas close to the height of the screen or higher than the screen
  32. // when one uses the offsets returned by 'compass2offsets'. On a SuperMac
  33. // Spectrum/8 Series II video card it was found that writing outside the screen
  34. // caused changes in the color look-up table.
  35. //
  36. class screenarea
  37. {
  38.     public:
  39.     
  40.         typedef enum screen_position
  41.         {
  42.             centered, left_side, right_side
  43.         };
  44.         
  45.         screenarea( int numbits, int xpos, int ypos);
  46.         screenarea( int numbits, screen_position where = centered);
  47.  
  48.         ~screenarea();
  49.  
  50.         int any_x() const;
  51.         int any_y() const;
  52.  
  53.         unsigned char *operator[]( const int index);
  54.         //
  55.         // add_circular_mask makes assumptions about the current color environment
  56.         // 'Try before you buy'. 941027: This also applies to 'add_rectangular_mask'
  57.         // 941123: Nowadays, the recommended way to obtain masks is by using simple
  58.         // Macintosh toolbox calls. 'fullscreen::fullscreen' can now make _all_
  59.         // colors associated with a window pmExplicit, so one can use
  60.         //
  61.         //        PmForeColor( 74);
  62.         //        PaintRect( &rect_to_fill_with_74s);
  63.         //
  64.         // to fill an area of the screen with the value '74'
  65.         //
  66.         void add_circular_mask( void);
  67.         void add_rectangular_mask( int mask_size);
  68.         void fill( const int value = 0);
  69.         void no_mask();
  70.         void add_fixation_dot( const int disparity = 0);
  71.         void add_stereo_cues( const int disparity = 0);
  72.         //
  73.         // compass2offsets takes a string containing zero or more dots and/or a
  74.         // series of letters denoting compass directions
  75.         // and turns it into a series of screen offsets.
  76.         // Every character of the string denotes a step in a state machine:
  77.         //
  78.         // eswn      : move a single pixel in the indicated direction
  79.         // ESWN      : include the offset of the current pixel in the list,
  80.         //            then move a single pixel in the indicated direction.
  81.         // .      : include the offset of the current pixel in the list, do not move.
  82.         // (the dot is included for human interface reasons. Generally one can replace
  83.         // .x by X, but .x may be easier to comprehend for some ('.e' shows
  84.         // the order 'set dot, then move', whereas 'E' doesn't)
  85.         //
  86.         // compass2offsets returns both a list of offsets and an integer denoting
  87.         // the length of that list. The 'maxnum' parameter is also used as input
  88.         // to signal the length of the 'offsets' array. As a special case one can
  89.         // pass zero to offsets. In that case maxnum is set to the size 'offsets'
  90.         // needs to have in order to accomodate the list specified by 'compass'.
  91.         // maxnum is always set to the actual number of offsets needed, but the
  92.         // offsets array is never filled with more offsets than were specified
  93.         // in the *maxnum parameter.
  94.         //
  95.         // Examples:
  96.         // .e.s.w.        creates 2x2 square dots to the bottom-right of the 'real' dot
  97.         // .e.n.w.        creates 2x2 square dots to the top-right of the 'real' dot
  98.         //
  99.         // ww.e.e.e.e.
  100.         // wwEEEE.
  101.         // wwEEEEN        these all create 5x1 bars centered at the dot position
  102.         // 2w4E.
  103.         // 12w10e4E.
  104.         //
  105.         void compass2offsets( const char *compass, int *offsets, int *maxnum);
  106.         
  107.     protected:
  108.         const int size;
  109.         const int half_size;
  110.         const int shiftbits;
  111.         const int coord_shift;
  112.         //
  113.         // 940823: compass2offsets needs to know rowBytes => it no longer is
  114.         // a local variable in 'init'. Instead it gets set by init.
  115.         //
  116.         int rowBytes;
  117.         //
  118.         // screen row pointers:
  119.         //
  120.         unsigned char **screen;
  121.         unsigned char *waste_area;
  122.  
  123.         static const int max_error;
  124.         void init( int xpos, int ypos);
  125.  
  126.     private:
  127.         //
  128.         // add_big_dot has not been debugged. It seems to work for use by
  129.         // add_fixation_dot and add_stereo_cues.
  130.         //
  131.         void add_big_dot( const int x, const int y, const int disparity = 0);
  132. };
  133.  
  134. inline unsigned char * screenarea::operator[]( const int index)
  135. {
  136.     return screen[ index];
  137. }
  138.  
  139. inline screenarea::screenarea( int numbits, int xpos, int ypos)
  140.     : size( 1 << numbits)
  141.     , half_size( 1 << (numbits - 1))
  142.     , shiftbits( 2 * numbits - 9)
  143.     , coord_shift( 16 - numbits)
  144. {
  145.     init( xpos, ypos);
  146. }
  147.  
  148. inline void screenarea::no_mask()
  149. {
  150.     fill( 128);
  151. }
  152.  
  153. inline int screenarea::any_x() const
  154. {
  155.     return (randomizer_step() % size) - half_size;
  156. }
  157.  
  158. inline int screenarea::any_y() const
  159. {
  160.     return (randomizer_step() % size) - half_size;
  161. }
  162.